<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/13
 * Time: 9:55
 */

namespace app\admin\controller;


use app\admin\model\AdminList;
use app\admin\model\AuthGroupAccess;
use app\admin\model\AuthGroup;
use app\admin\model\AuthRule;
use think\facade\Env;

class Admin extends Base
{

    public function adminList(){
        return $this->fetch();
    }

    public function adminData($page,$limit){
        $data = AdminList::order('create_time asc')->page($page,$limit)->select()->each(function ($item){
            $item->group = AuthGroup::where('id',AuthGroupAccess::where('uid',$item->id)->value('group_id'))->value('title');
        })->toArray();
        $count = AdminList::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }

    public function adminAdd(){
        if ($this->request->isPost()){
            $post = input('post.');
            $validate = $this->validate($post,'app\admin\validate\Admin.register');
            if (true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $data = [
                'username'=>$post['username'],
                'password'=>md5($post['username'].md5($post['password'])."adminFly"),
                'status'=>$post['status'],
                'create_time'=>time()
            ];
            $admin_id = AdminList::insertGetId($data);
            $ret = AuthGroupAccess::create(['uid'=>$admin_id,'group_id'=>$post['group_id']]);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $data = AuthGroup::where('status',1)->field('id,title')->select()->toArray();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function adminEdit($id){
        if ($this->request->isPost()){
            $post = input('post.');
            if ($post['password']){
                $validate = $this->validate($post,'app\admin\validate\Admin.edit');
                if (true !== $validate){
                    return json(['code'=>0,'msg'=>$validate]);
                }
                AdminList::update(['password'=>md5($post['username'].md5($post['password'])."adminFly")],['id'=>$post['id']]);
            }
            $ret = AuthGroupAccess::update(['group_id'=>$post['group_id']],['uid'=>$post['id']]);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $admin = AdminList::where('id',$id)->find();
        $data = AuthGroup::where('status',1)->field('id,title')->select()->toArray();
        $this->assign('data',$data);
        $this->assign('admin',$admin);
        return $this->fetch();
    }
    public function adminStatus(){
        $post = input('post.');
        AdminList::update([$post['name']=>$post['value']],['id'=>$post['id']]);
        return 1;
    }
    public function adminDel(){
        $id = input('post.id');
        $ret = AdminList::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>Env::get('msg.del_error')]);
        }
        return json(['code'=>1,'msg'=>Env::get('msg.del_success')]);
    }
    public function groupList(){
        return $this->fetch();
    }
    public function groupData($page,$limit){
        $where = input('get.where')?[['title','like','%'.trim(input('get.where')).'%']]:'';
        $data = AuthGroup::where($where)->page($page,$limit)->select()->each(function ($item){
            $item->rules = implode(',',AuthRule::where('id','in',explode(',',$item->rules))->column('title'));
        })->toArray();
        $count = AuthGroup::where($where)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function groupAdd(){
        if ($this->request->isPost()){
            $post = input('post.');
            $validate = $this->validate($post,'app\admin\validate\Groups.add');
            if (true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $data = [
                'title'=>$post['title'],
                'rules'=>implode(',',$post['rules']),
                'status'=>$post['status'],
            ];
            $ret = AuthGroup::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $rules = AuthRule::where('status',1)->field('id,pid,title')->select()->toArray();
        $res = $this->roleArray($rules);
        $this->assign('rules',$res);
        return $this->fetch();
    }
    public function groupEdit($id){
        if ($this->request->isPost()){
            $post = input('post.');
            $validate = $this->validate($post,'app\admin\validate\Groups.add');
            if (true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $data = [
                'title'=>$post['title'],
                'rules'=>implode(',',$post['rules']),
                'status'=>$post['status'],
            ];
            $ret = AuthGroup::update($data,['id'=>$post['id']]);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $data = AuthGroup::where('id',$id)->find();
        $data['rules'] = explode(',',$data['rules']);
        $rules = AuthRule::where('status',1)->field('id,pid,title')->select()->toArray();
        $res = $this->roleArray($rules);
        $this->assign('rules',$res);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function groupDel(){
        $id = input('post.id');
        $ret = AuthGroup::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>Env::get('msg.del_error')]);
        }
        return json(['code'=>1,'msg'=>Env::get('msg.del_success')]);
    }
    public function groupStatus(){
        $post = input('post.');
        AuthGroup::update([$post['name']=>$post['value']],['id'=>$post['id']]);
        return 1;
    }
    public function ruleList(){
        return $this->fetch();
    }
    public function ruleData($page,$limit){
        $where = input('get.where')?[['title','like','%'.trim(input('get.where')).'%']]:'';
        $data = AuthRule::where($where)->page($page,$limit)->select()->each(function ($item){
            $item->p_title = $item->pid?AuthRule::where('id',$item->pid)->value('title'):'顶级权限';
            $item->types_text = $item->types == 1?'菜单':'按钮';
        })->toArray();
        $count = AuthRule::where($where)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function ruleAdd(){
        if ($this->request->isPost()){
            $post = input('post.');
            $validate = $this->validate($post,'app\admin\validate\Auth.add');
            if (true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $data = [
                'name'=>$post['name'],
                'title'=>$post['title'],
                'pid'=>(int)$post['pid'],
                'condition'=>(string)$post['condition'],
                'types'=>$post['types'],
                'status'=>$post['status'],
                'sort'=>$post['sort'],
            ];
            $ret = AuthRule::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $pid  = AuthRule::where('pid',0)->field('id,title')->select()->toArray();
        $this->assign('pid',$pid);
        return $this->fetch();
    }
    public function ruleEdit($id){
        if ($this->request->isPost()){
            $post = input('post.');
            $validate = $this->validate($post,'app\admin\validate\Auth.add');
            if (true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $data = [
                'name'=>$post['name'],
                'title'=>$post['title'],
                'pid'=>(int)$post['pid'],
                'condition'=>(string)$post['condition'],
                'types'=>$post['types'],
                'status'=>$post['status'],
                'sort'=>$post['sort'],
            ];
            $ret = AuthRule::update($data,['id'=>$post['id']]);
            if (!$ret){
                return json(['code'=>0,'msg'=>Env::get('msg.add_error')]);
            }
            return json(['code'=>1,'msg'=>Env::get('msg.add_success')]);
        }
        $data = AuthRule::where('id',$id)->find();
        $pid  = AuthRule::where('pid',0)->field('id,title')->select()->toArray();
        $this->assign('data',$data);
        $this->assign('pid',$pid);
        return $this->fetch();
    }
    public function ruleStatus(){
        $post = input('post.');
        AuthRule::update([$post['name']=>$post['value']],['id'=>$post['id']]);
        return 1;
    }
    public function ruleDel(){
        $id = input('post.id');
        $ret = AuthRule::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>Env::get('msg.del_error')]);
        }
        return json(['code'=>1,'msg'=>Env::get('msg.del_success')]);
    }

}